package com.wc.alorithm_luogu._BFS.Corn_Maze_S;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

/**
 * @Author congge
 * @Date 2024/5/13 20:50
 * @description https://www.luogu.com.cn/problem/P1825
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 310;
    static char[][] g = new char[N][];
    static boolean[][] st = new boolean[N][N];
    static Map<Integer, Integer> map = new HashMap<>();
    static int[] dx = new int[]{-1, 1, 0, 0}, dy = new int[]{0, 0, 1, -1};
    static int[][] a = new int[26][5];
    static int n, m, sx, sy, ex, ey;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            g[i] = (" " + sc.next()).toCharArray();
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (g[i][j] >= 'A' && g[i][j] <= 'Z') {
                    int le = g[i][j] - 'A';
                    a[le][++a[le][0]] = i;
                    a[le][++a[le][0]] = j;
                } else if (g[i][j] == '=') {
                    ex = i;
                    ey = j;
                } else if (g[i][j] == '@') {
                    sx = i;
                    sy = j;
                }
            }
        }
        for (int i = 0; i < 26; i++) {
            if (a[i][0] > 0) {
                int x1 = a[i][1], y1 = a[i][2];
                int x2 = a[i][3], y2 = a[i][4];
                int p1 = x1 * N + y1, p2 = x2 * N + y2;
                map.put(p1, p2);
                map.put(p2, p1);
            }
        }
        int res = bfs();
        out.println(res);
        out.flush();
    }

    static int bfs() {
        Queue<Integer> q = new LinkedList<>();
        q.add(sx);
        q.add(sy);
        q.add(0);
        while (!q.isEmpty()) {
            int x = q.poll(), y = q.poll(), t = q.poll();
            if (x == ex && y == ey) return t;
            if (g[x][y] >= 'A' && g[x][y] <= 'Z') {
                int p1 = map.get(x * N + y);
                x = p1 / N;
                y = p1 % N;
            }
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i], ny = y + dy[i];
                if (!isBorder(nx, ny)) {
                    if (g[nx][ny] != '#' && !st[nx][ny]) {
                        st[nx][ny] = true;
                        q.add(nx);
                        q.add(ny);
                        q.add(t + 1);
                    }
                }
            }
        }
        return -1;
    }

    static boolean isBorder(int x, int y) {
        return x < 1 || x > n || y < 1 || y > m;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
